/* enum */
import ComponentNameEnum from '@model/enum/ComponentNameEnum'
/* model */
import { ONLY_WORK_WX_CHAT } from '@src/model/const/Alert'
/* vue */
import VC from '@model/VC'
import { Component, Emit, Prop } from 'vue-property-decorator'
import { CreateElement } from 'vue'
/* platform */
import PlatformExtension from '@src/platform/extension'
/* util */
import Platform, { isQYWX } from '@src/util/platform'
/* scss */
import '@src/component/business/BizWxChatButton/BizWxChatButton.scss'

enum BizWxChatButtonEventEnum {
  Click = 'click'
}

@Component({ 
  name: ComponentNameEnum.BizWxChatButton
})
export default class BizWxChatButton extends VC {
  
  @Prop({ default: '' }) readonly customerId: string | undefined
  @Prop({ default: '' }) readonly userId: string | undefined
  
  @Emit(BizWxChatButtonEventEnum.Click)
  private onClickHandler(): void {
    // do some things
  }
  
  /**
   * @description: 发送微信消息
   * @return {void}
   */
  private sendWeiXinMessage(): Promise<boolean | string | void> | void {
    console.log('sendWeiXinMessage')
    // 非企业微信客户端提示不支持此功能
    if (!isQYWX()) {
      console.log('isQYWX')
      return Platform.alert(ONLY_WORK_WX_CHAT)
    }
    
    // 客户列表页面单独处理
    if (this.customerId) {
      console.log('customerId')
      return this.onClickHandler()
    }
    
    // 只有企业微信外部用户id，则打开单人聊天
    if (this.userId) {
      console.log('userId')
      return PlatformExtension.chat.openEnterpriseChatForSignle(this.userId)
    }
  }
  
  /**
   * @description: 发送微信消息
   * -- 支持外部调用的
   * @return {void}
   */
  public outsideSendWeiXinMessage(): void {
    this.sendWeiXinMessage()
  }
  
  render(h: CreateElement) {
    return (
      <div class={ComponentNameEnum.BizWxChatButton} onClick={() => this.sendWeiXinMessage()}>
        <i class='iconfont icon-weixin'></i>
      </div>
    )
  }
}